/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csse3004.GD.PM;

import csse3004.GD.DA.PollMasterDA;
import csse3004.GD.Question;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 *
 * @author David Bullock 42062222
 */
public class showPollController implements Controller {

    protected final Log logger = LogFactory.getLog(getClass());
    private PollMasterDA service = new PollMasterDA();

    public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) {
        try {
            logger.info("starting to process showpoll");
            ModelAndView mav = new ModelAndView("showPoll");
            int pid = Integer.parseInt(hsr.getParameter("pid"));
            //the following two lines make it revert to test data if the database funciton fails            
            double[] location = service.getLocation(pid);
            location = (location == null) ? getLocation(pid) : location;
            
            double lat = location[0];
            double lon = location[1];

            if (hsr.getParameter("qid") != null) {
                int qid = Integer.parseInt(hsr.getParameter("qid"));
                if (hsr.getParameter("public") != null) {
                    service.setQuestionPublic(qid, !Boolean.parseBoolean(hsr.getParameter("public")));
                } else if (hsr.getParameter("seconds") != null) {//open the question
                    service.openQuestion(qid, Integer.parseInt(hsr.getParameter("seconds")));
                } else {
                    logger.info(service.closeQuestion(qid));
                }
            }

            if (hsr.getParameter("lon") != null && hsr.getParameter("lat") != null) {
                lon = Double.parseDouble(hsr.getParameter("lon"));
                lat = Double.parseDouble(hsr.getParameter("lat"));

                if (service.addLocationToPoll(pid, lon, lat) == false) {
                    logger.info("There was an error setting the Location");
                    mav.addObject("error", "There was an error setting the location. Please try again");
                }
            }

            ArrayList<Question> Questions = (service.database) ? service.getQuestions(pid) : new Question().genTestQuestion(pid);

            lat = location[0];
            lon = location[1];
            mav.addObject("questions", Questions);

            mav.addObject("lon", lon);
            mav.addObject("lat", lat);

            mav.addObject("pid", pid);
            mav.addObject("set", true);
            return mav;
        } catch (NumberFormatException E) {
            return new ModelAndView("showPoll", "set", false);
        }
    }

    private double[] getLocation(int pid) {
        double[] temp = new double[2];
        temp[1] = 153.012772;
        temp[0] = -27.477207203406262;
        return temp;
    }
}
